﻿
app.controller('ConstructionController', function ($scope, $http, $window, $compile, $filter, ipCookie, common, option, Upload) {
    $scope.result = {};
    $scope.dateOption = option.date;
    $scope.datetimeOption = option.datetime;


    $scope.BindDepartmentList = function (control, defaultvalue) {
        $scope.PageLoading = true;
        $http({
            method: 'POST',
            url: '../SystemConfig/GetDepartmentList'
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $.each(ret.Data, function (i, n) {
                        if (n.DepartmentName != "系统支持") {
                            if (defaultvalue != null) {
                                if (n.Id == defaultvalue)
                                    $("#" + control).append($compile("<option selected='selected' value=\"" + n.Id + "\">" + n.DepartmentName + "</option>")($scope));
                                else
                                    $("#" + control).append($compile("<option value=\"" + n.Id + "\">" + n.DepartmentName + "</option>")($scope));
                            }
                            else {
                                $("#" + control).append($compile("<option value=\"" + n.Id + "\">" + n.DepartmentName + "</option>")($scope));
                            }
                        }
                    })
                    $("#" + control).selectpicker('refresh');
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }
    $scope.InitLocationModel = function (list, model) {
        var obj = {
            "LocationId": "",
            "LocationDetail": "",
            "X": "",
            "Y": ""
        };
        list.push(obj);
        var controlid = "Location_Select_" + (list.length - 1);
        setTimeout(function () {
            $scope.GetLocationList(model.FactoryAreaId, controlid);
        }, 200);
    }
    $scope.RemoveLocationItem = function (model, list) {
        let index = list.indexOf(model);
        list.splice(index, 1);
    }

    $scope.GetCurrentLocationModel = function (model) {
        $scope.CurrentLocationModel = model;
    }

    $scope.ChangeFactoryLocation = function (id) {
        var controls = $('[id^="Location_Select_"]');
        for (var i = 0; i < controls.length; i++) {
            $scope.GetLocationList(id, 'Location_Select_' + i);
        }
    }

    $scope.GetConstructionType = function () {
        $scope.DataLoading = true;
        $scope.TypeList = [];
        $http({
            method: 'POST',
            url: '../Construction/GetConstructionType'
        })
            .success(function (ret) {
                $scope.DataLoading = false;
                common.success($scope, null, ret);
                $scope.TypeList = ret.Data;
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });
    }

    $scope.GetConstructionDocType = function () {
        $scope.DataLoading = true;
        $scope.DocTypeList = [];
        $http({
            method: 'POST',
            url: '../Construction/GetConstructionDocType'
        })
            .success(function (ret) {
                $scope.DataLoading = false;
                common.success($scope, null, ret);
                $scope.DocTypeList = ret.Data;
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });
    }
    $scope.GetConstructionDetail = function (Id) {
        $scope.Loading = true;
        $scope.DetailModel = {};
        $http({
            method: 'POST',
            url: '../Construction/GetConstructionDetail',
            data: { "Id": Id }
        })
            .success(function (ret) {
                $scope.Loading = false;
                common.success($scope, null, ret);
                $scope.DetailModel = ret.Data;
                var typeArray = $scope.DetailModel.TypeId.split(',');
                setTimeout(function () {
                    for (var i = 0; i < typeArray.length; i++) {
                        $("#chk_type_" + typeArray[i]).attr("checked", true);
                    }
                    $("#txt_type_" + 5).val($scope.DetailModel.OtherType);
                    for (var i = 0; i < typeArray.length; i++) {
                        $("#chk_type2_" + typeArray[i]).attr("checked", true);
                    }
                }, 2000);
                var docTypeArray = $scope.DetailModel.DocTypeId.split(',');
                setTimeout(function () {
                    for (var i = 0; i < docTypeArray.length; i++) {
                        $("#chk_doctype_" + docTypeArray[i]).attr("checked", true);
                    }
                    $("#txt_doctype_" + 6).val($scope.DetailModel.OtherDocType);


                    for (var i = 0; i < docTypeArray.length; i++) {
                        $("#chk_doctype2_" + docTypeArray[i]).attr("checked", true);
                    }
                }, 2000);

                var isoutdoorArray = $scope.DetailModel.IsOutdoor.split(',');
                setTimeout(function () {
                    for (var i = 0; i < isoutdoorArray.length; i++) {
                        $("#IsOutdoor_" + isoutdoorArray[i]).attr("checked", true);
                    }

                    for (var i = 0; i < isoutdoorArray.length; i++) {
                        $("#IsOutdoor2_" + isoutdoorArray[i]).attr("checked", true);
                    }
                }, 2000);
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });
    }


    $scope.PreSubmitConstructionForm = function (model) {
        //1 检查是否有未填字段
        if (model.ProjectName == null || model.ProjectName == '') {
            alertify.alert("请输入项目名称！");
            return false;
        }
        if (model.DepartmentId == null) {
            alertify.alert("请选择部门！");
            return false;
        } if (model.FactoryAreaId == null || model.FactoryAreaId == '') {
            alertify.alert("请选择厂区！");
            return false;
        }
        if (model.ApplyDate == null || model.ApplyDate == '') {
            alertify.alert("请填写申请日期！");
            return false;
        }
        if (model.CompanyId == null || model.CompanyId == '') {
            alertify.alert("请选择施工单位！");
            return false;
        }
        if (model.Reponsible == null || model.Reponsible == '') {
            alertify.alert("请输入施工单位现场负责人！");
            return false;
        }
        if (model.Phone == null || model.Phone == '') {
            alertify.alert("请输入施工单位现场负责人联系方式！");
            return false;
        }
        if (model.ApplyDepContract == null || model.ApplyDepContract == '') {
            alertify.alert("请输入申请部门现场监护人！");
            return false;
        }
        if (model.ApplyDepPhone == null || model.ApplyDepPhone == '') {
            alertify.alert("请输入申请部门现场监护人联系方式！");
            return false;
        }
        if (model.LocalDepContract == null || model.LocalDepContract == '') {
            alertify.alert("请输入属地联系人！");
            return false;
        }
        if (model.LoalDepPhone == null || model.LoalDepPhone == '') {
            alertify.alert("请输入属地联系人联系方式！");
            return false;
        }
        if (model.Content == null || model.Content == '') {
            alertify.alert("请输入施工内容！");
            return false;
        }
        if (model.StartTime == null || model.StartTime == '') {
            alertify.alert("请输入施工开始时间！");
            return false;
        }
        if (model.EndTime == null || model.EndTime == '') {
            alertify.alert("请输入施工结束时间！");
            return false;
        }
        if ($scope.LocationModel == null || $scope.LocationModel.length == 0) {
            alertify.alert("请添加施工位置！");
            return false;
        }
        else {
            for (var i = 0; i < $scope.LocationModel.length; i++) {
                if ($scope.LocationModel[i].LocationId == null || $scope.LocationModel[i].LocationId == '') {
                    alertify.alert("请完善添加的施工位置信息！");
                    return false;
                }
                if ($scope.LocationModel[i].LocationDetail == null || $scope.LocationModel[i].LocationDetail == '') {
                    alertify.alert("请完善添加的施工详细位置信息！");
                    return false;
                }
               
            }
        }

        var typelist = $('[id^="chk_type_"]');
        var types = '';
        model.OtherType = '';
        for (var i = 0; i < typelist.length; i++) {
            if ($(typelist[i]).prop("checked")) {
                types += typelist[i].id.replace("chk_type_", "") + ",";
            }
        }

        if (types == '') {
            alertify.alert("请选择涉及危险作业种类！");
            return false;
        }

        var doctypelist = $('[id^="chk_doctype_"]');
        var doctypes = '';
        model.OtherDocType = '';
        for (var i = 0; i < doctypelist.length; i++) {
            if ($(doctypelist[i]).prop("checked")) {
                doctypes += doctypelist[i].id.replace("chk_doctype_", "") + ",";
            }
        }
        if (doctypes == '') {
            alertify.alert("请选择提交材料类型！");
            return false;
        }


        var isoutdoor = '';
        var isoutdoorlist = $('[id^="IsOutdoor_"]');
        for (var i = 0; i < isoutdoorlist.length; i++) {
            if ($(isoutdoorlist[i]).prop("checked")) {
                isoutdoor += isoutdoorlist[i].id.replace("IsOutdoor_", "") + ",";
            }
        }
        if (isoutdoor == '') {
            alertify.alert("请选择室内或室外施工！");
            return false;
        }
        $("#sub").attr("href", "#NoticeModal");
        $("#sub").attr("data-toggle", "modal");
        $("#sub").click();
    }

    $scope.AddConstructionForm = function (model, status, page) {

        model.Status = status;
        var typelist = $('[id^="chk_type_"]');
        var types = '';
        model.OtherType = '';
        for (var i = 0; i < typelist.length; i++) {
            if ($(typelist[i]).prop("checked")) {
                types += typelist[i].id.replace("chk_type_", "") + ",";
            }
        }
        for (var i = 0; i < $scope.TypeList.length; i++) {
            if ($scope.TypeList[i].IsInput) {
                model.OtherType += $("#txt_type_" + $scope.TypeList[i].Id).val();
            }
        }

        var doctypelist = $('[id^="chk_doctype_"]');
        var doctypes = '';
        model.OtherDocType = '';
        for (var i = 0; i < doctypelist.length; i++) {
            if ($(doctypelist[i]).prop("checked")) {
                doctypes += doctypelist[i].id.replace("chk_doctype_", "") + ",";
            }
        }
        for (var i = 0; i < $scope.DocTypeList.length; i++) {
            if ($scope.DocTypeList[i].IsInput) {
                model.OtherDocType += $("#txt_doctype_" + $scope.DocTypeList[i].Id).val();
            }
        }
        model.DocTypeId = doctypes;
        model.TypeId = types;
        var isoutdoor = '';
        var isoutdoorlist = $('[id^="IsOutdoor_"]');
        for (var i = 0; i < isoutdoorlist.length; i++) {
            if ($(isoutdoorlist[i]).prop("checked")) {
                isoutdoor += isoutdoorlist[i].id.replace("IsOutdoor_", "") + ",";
            }
        }
        model.IsOutdoor = isoutdoor;

        var files = document.querySelector('input[type=file]').files;
        $scope.DataLoading = true;
        $("#btn_close_notice").click();
        Upload.upload({
            method: 'POST',
            url: '../Construction/AddConstructionForm',
            data: { 'model': model, "LocationList": $scope.LocationModel },
            file: files
        }).success(function (ret) {
            $scope.DataLoading = false;
            if (ret.Code == 100) {

                if (model.Status == 0) {
                    alertify.alert("保存成功！");
                }
                else {
                    alertify.alert("提交成功！");
                }

                setTimeout(function () {
                    window.location.href = "ConstructionFormPageList?page=" + page;
                }, 3000);
            } else
                alertify.alert(ret.Msg);
        })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                common.error($scope);
            })
    }

    $scope.refreshOption = function (control) {
        $("#" + control).selectpicker('refresh');
    }

    $scope.GetConstructionFormPageList = function (model, page, isload) {
        $scope.DataList = [];
        $scope.DataLoading = true;
        var condition = JSON.parse(sessionStorage.getItem(window.location.pathname));
        if (isload && condition) {
            model = condition;
            $scope.SearchModel = condition;
        } else {
            sessionStorage.setItem(window.location.pathname, JSON.stringify(model));
        }
        $http({
            method: 'POST',
            url: '../Construction/GetConstructionFormPageList',
            data: { 'model': model, 'page': page }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    $scope.TotalCount = ret.Data.TotalItemCount;
                    $scope.DataList = ret.Data.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                alertify.alert("请检查网络");
            });

        $http({
            method: 'POST',
            url: '../Construction/GetConstructionTaskCount',
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    $scope.TaskCount = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                alertify.alert("请检查网络");
            });
    }

    $scope.GetConstructionFileList = function (Id) {
        $scope.FileList = [];
        $http({
            method: 'POST',
            url: '../Construction/GetConstructionFormFileList',
            data: { 'Id': Id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.FileList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                alertify.alert("请检查网络");
            });
    }



    $scope.GetConstructionFormLocation = function (Id, factory) {
        $scope.LocationModel = [];
        $http({
            method: 'POST',
            url: '../Construction/GetConstructionFormLocation',
            data: { 'FormId': Id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.LocationModel = ret.Data;
                    if (ret.Data.length == 0) {
                        $scope.InitLocationModel($scope.LocationModel, $scope.EditModel)
                    }
                    setTimeout(
                        function () {
                            for (var i = 0; i < $scope.LocationModel.length; i++) {
                                $scope.GetLocationList(factory, 'Location_Select_' + i, $scope.LocationModel[i].LocationId);
                            }
                        }, 500
                    );


                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                alertify.alert("请检查网络");
            });
    }

    $scope.AddConstructionCompany = function (model) {
        $http({
            method: 'POST',
            url: '../Construction/AddConstructionCompany',
            data: { 'model': model }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    alertify.alert(ret.Msg);
                    $("#btn_close").click();
                    $scope.GetConstructionCompany('Company_Select');
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                alertify.alert("请检查网络");
            });
    }
    $scope.AddFireOperator = function (model) {
        $http({
            method: 'POST',
            url: '../Construction/AddFirOperateWorker',
            data: { 'model': model }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    alertify.alert(ret.Msg);
                    $("#btn_close2").click();
                    $scope.GetFireOperateWorker('Worker_Select', $("#Worker_Select").val());
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                alertify.alert("请检查网络");
            });
    }

    $scope.GetConstructionForm_Edit = function (Id) {
        $scope.Loading = true;
        $scope.EditModel = {};
        $http({
            method: 'POST',
            url: '../Construction/GetConstructionForm',
            data: { "FormId": Id }
        })
            .success(function (ret) {
                $scope.Loading = false;
                common.success($scope, null, ret);
                $scope.EditModel = ret.Data;
                $scope.EditModel.FactoryAreaId = $scope.EditModel.FactoryAreaId + '';
                $scope.BindDepartmentList('Dep_Select', $scope.EditModel.DepartmentId);
                $scope.BindDepartmentList('LocalDep_Select', $scope.EditModel.LocalDepId);
                $scope.GetLocationList($scope.EditModel.FactoryAreaId, 'Location_Select', $scope.EditModel.LocationId);
                $scope.GetConstructionCompany('Company_Select', $scope.EditModel.CompanyId);
                $scope.EditModel.ApplyDate = $filter('JSONDate')($scope.EditModel.ApplyDate, 'yyyy-MM-dd');
                $scope.EditModel.StartTime = $filter('JSONDate')($scope.EditModel.StartTime, 'yyyy-MM-dd');
                $scope.EditModel.EndTime = $filter('JSONDate')($scope.EditModel.EndTime, 'yyyy-MM-dd');
                $scope.EditModel.CreateTime = $filter('JSONDate')($scope.EditModel.CreateTime, 'yyyy-MM-dd HH:mm:ss');
                if ($scope.EditModel.TypeId != null) {
                    var typeArray = $scope.EditModel.TypeId.split(',');
                    setTimeout(function () {
                        for (var i = 0; i < typeArray.length; i++) {
                            $("#chk_type_" + typeArray[i]).attr("checked", true);
                        }
                        $("#txt_type_" + 5).val($scope.EditModel.OtherType);
                    }, 2000);
                }
                if ($scope.EditModel.DocTypeId != null) {
                    var docTypeArray = $scope.EditModel.DocTypeId.split(',');
                    setTimeout(function () {
                        for (var i = 0; i < docTypeArray.length; i++) {
                            $("#chk_doctype_" + docTypeArray[i]).attr("checked", true);
                        }
                        $("#txt_doctype_" + 6).val($scope.EditModel.OtherDocType);
                    }, 2000);
                }
                if ($scope.EditModel.IsOutdoor != null) {
                    var isoutdoorArray = $scope.EditModel.IsOutdoor.split(',');
                    setTimeout(function () {
                        for (var i = 0; i < isoutdoorArray.length; i++) {
                            $("#IsOutdoor_" + isoutdoorArray[i]).attr("checked", true);
                        }
                    }, 2000);
                }

                $scope.GetApproveComment($scope.EditModel.Status);

                $scope.GetConstructionFormLocation(Id, $scope.EditModel.FactoryAreaId);
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });
    }

    $scope.EditConstructionForm = function (model, status, page) {
        model.Status = status;
        var typelist = $('[id^="chk_type_"]');
        var types = '';
        model.OtherType = '';
        for (var i = 0; i < typelist.length; i++) {
            if ($(typelist[i]).prop("checked")) {
                types += typelist[i].id.replace("chk_type_", "") + ",";
            }
        }
        for (var i = 0; i < $scope.TypeList.length; i++) {
            if ($scope.TypeList[i].IsInput) {
                model.OtherType += $("#txt_type_" + $scope.TypeList[i].Id).val();
            }
        }

        var doctypelist = $('[id^="chk_doctype_"]');
        var doctypes = '';
        model.OtherDocType = '';
        for (var i = 0; i < doctypelist.length; i++) {
            if ($(doctypelist[i]).prop("checked")) {
                doctypes += doctypelist[i].id.replace("chk_doctype_", "") + ",";
            }
        }
        for (var i = 0; i < $scope.DocTypeList.length; i++) {
            if ($scope.DocTypeList[i].IsInput) {
                model.OtherDocType += $("#txt_doctype_" + $scope.DocTypeList[i].Id).val();
            }
        }
        model.DocTypeId = doctypes;
        model.TypeId = types;
        var isoutdoor = '';
        var isoutdoorlist = $('[id^="IsOutdoor_"]');
        for (var i = 0; i < isoutdoorlist.length; i++) {
            if ($(isoutdoorlist[i]).prop("checked")) {
                isoutdoor += isoutdoorlist[i].id.replace("IsOutdoor_", "") + ",";
            }
        }
        model.IsOutdoor = isoutdoor;
        var files = document.querySelector('input[type=file]').files;
        $scope.DataLoading = true;
        $("#btn_close_notice").click();
        Upload.upload({
            method: 'POST',
            url: '../Construction/EditConstructionForm',
            data: { 'model': model, "LocationList": $scope.LocationModel },
            file: files
        }).success(function (ret) {
            $scope.DataLoading = false;
            if (ret.Code == 100) {
                if (model.Status == 0) {
                    alertify.alert("保存成功！");
                }
                else {
                    alertify.alert("提交成功！");
                }
                setTimeout(function () {
                    window.location.href = "ConstructionFormPageList?page=" + page;
                }, 3000);
            } else
                alertify.alert(ret.Msg);
        })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                common.error($scope);
            })
    }

    $scope.DeleteConstructionForm = function (id, searchmodel, page) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认此操作？", function (ev) {
                ev.preventDefault();
                $scope.PageLoading = true;
                $http({
                    method: 'POST',
                    url: '../Construction/DeleteConstructionForm',
                    data: { 'Id': id }
                })
                    .success(function (ret) {
                        common.success($scope, null, ret);
                        $scope.PageLoading = false;
                        if (ret.Code == 100) {
                            $scope.AlertBox("删除成功！");
                            $scope.GetConstructionFormPageList(searchmodel, page);
                        }
                        else {
                            alertify.alert(ret.Msg);
                        }
                    })
                    .error(function (data, status, headers, config) {
                        alertify.alert("请检查网络");
                    });
            }, function (ev) {
                ev.preventDefault();
            });
    }

    $scope.WithdrawForm = function (id, type, model, page) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认撤回？", function (ev) {
                ev.preventDefault();
                $scope.PageLoading = true;
                $http({
                    method: 'POST',
                    url: '../Construction/WithdrawApply',
                    data: { 'FormId': id, 'Type': type }
                })
                    .success(function (ret) {
                        common.success($scope, null, ret);
                        $scope.PageLoading = false;
                        if (ret.Code == 100) {
                            $scope.AlertBox("撤回成功！");
                            if (type == 1) {
                                $scope.GetConstructionFormPageList(model, page);
                            }
                            else if (type == 2) {
                                $scope.GetFireOperateFormPageList(model, page);
                            }
                            else if (type == 3) {
                                $scope.GetClimbOperateFormPageList(model, page);
                            }
                            else if (type == 4) {
                                $scope.GetLimitSpaceOperateFormPageList(model,page);
                            }
                        }
                        else {
                            alertify.alert(ret.Msg);
                        }
                    })
                    .error(function (data, status, headers, config) {
                        alertify.alert("请检查网络");
                    });
            }, function (ev) {
                ev.preventDefault();
            });
    }
    $scope.DeleteConstructionFile = function (id, formId) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认删除文件？", function (ev) {
                ev.preventDefault();
                $scope.PageLoading = true;
                $http({
                    method: 'POST',
                    url: '../Construction/DeleteConstructionFile',
                    data: { 'Id': id }
                })
                    .success(function (ret) {
                        common.success($scope, null, ret);
                        $scope.PageLoading = false;
                        if (ret.Code == 100) {
                            $scope.AlertBox("删除成功！");
                            $scope.GetConstructionFileList(formId);
                        }
                        else {
                            alertify.alert(ret.Msg);
                        }
                    })
                    .error(function (data, status, headers, config) {
                        alertify.alert("请检查网络");
                    });
            }, function (ev) {
                ev.preventDefault();
            });
    }

    $scope.GetApproveLogList = function (Id) {
        $scope.ApproveLog = [];
        $http({
            method: 'POST',
            url: '../Construction/GetApproveLogList',
            data: { 'FormId': Id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.ApproveLog = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                alertify.alert("请检查网络");
            });
    }

    $scope.ApproveConstruction = function (model, status, page) {
        var text = "通过";
        if (status == "false") {
            text = "驳回";
            if (model.Remark == null || model.Remark == "") {
                alertify.alert("请填写意见！");
                return false;
            }
        }
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认" + text + "该施工申请？", function (ev) {
                ev.preventDefault();
                $scope.Loading = true;
                model.IsApprove = status;
                $http({
                    method: 'POST',
                    url: '../Construction/ApproveConstructionForm',
                    data: { 'model': model }
                })
                    .success(function (ret) {
                        common.success($scope, null, ret);
                        $scope.Loading = false;
                        if (ret.Code == 100) {
                            alertify.alert(ret.Msg);
                            setTimeout(function () {
                                window.location.href = "ConstructionFormPageList?page=" + page;
                            }, 2000);

                        }
                        else {
                            alertify.alert(ret.Msg);
                        }
                    })
                    .error(function (data, status, headers, config) {
                        alertify.alert("请检查网络");
                    });
            }, function (ev) {
                ev.preventDefault();
            });
    }
    $scope.ApproveFireOperateForm = function (model, status, page) {
        var text = "通过";
        if (status == "false") {
            text = "驳回";
            if (model.Remark == null || model.Remark == "") {
                alertify.alert("请填写意见！");
                return false;
            }
        }
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认" + text + "该动火申请？", function (ev) {
                ev.preventDefault();
                $scope.Loading = true;
                model.IsApprove = status;
                $http({
                    method: 'POST',
                    url: '../Construction/ApproveFireOperateForm',
                    data: { 'model': model }
                })
                    .success(function (ret) {
                        common.success($scope, null, ret);
                        $scope.Loading = false;
                        if (ret.Code == 100) {
                            alertify.alert(ret.Msg);
                            setTimeout(function () {
                                window.location.href = "FireOperateFormPageList?page=" + page;
                            }, 2000);

                        }
                        else {
                            alertify.alert(ret.Msg);
                        }
                    })
                    .error(function (data, status, headers, config) {
                        alertify.alert("请检查网络");
                    });
            }, function (ev) {
                ev.preventDefault();
            });
    }
    $scope.StateList = function (isshow) {
        $scope.PageLoading = true;
        $scope.StateList = [];
        $http({
            method: 'POST',
            url: '../Construction/GetStatusList'
        }).success(function (ret) {
            $scope.PageLoading = false;
            if (ret.Code == 100) {
                $scope.StateList = ret.Data;
                $scope.slist = [];
                for (var i = 0; i < $scope.StateList.length; i++) {
                    $scope.slist.push(ret.Data[i].StatusName);
                }
                if (isshow) {
                    $scope.MyStep = $("#step");
                    $scope.MyStep.step({
                        index: -1,
                        time: 500,
                        title: $scope.slist
                    });
                }
            } else
                alertify.alert(ret.Msg);
        }).error(function (data, status, headers, config) {
            common.error($scope);
            $scope.PageLoading = false;
            alertify.alert("请检查网络");
        })
    }

    $scope.ShowStep = function (State) {
        $("#step").html("");
        $scope.MyStep = $("#step");
        $scope.MyStep.step({
            index: -1,
            time: 500,
            title: $scope.slist
        });
        $scope.MyStep.toStep(State);
    }

    $scope.GetConstructionList = function () {
        $scope.ConstructionList = [];
        $http({
            method: 'POST',
            url: '../Construction/GetConstructionList'
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.ConstructionList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                alertify.alert("请检查网络");
            });
    }
    $scope.GetTodayFireOperateList = function (department) {
        $scope.TodayFireOperateList = [];
        $http({
            method: 'POST',
            url: '../Construction/GetTodayFireOperateList',
            data: { "DepId": department}
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.TodayFireOperateList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                alertify.alert("请检查网络");
            });
    }
    $scope.GetTodayClimbList = function (department) {
        $scope.TodayClimbFormList = [];
        $http({
            method: 'POST',
            url: '../Construction/GetTodayClimbList',
            data: { "DepId": department }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.TodayClimbFormList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                alertify.alert("请检查网络");
            });
    }
    $scope.GetTodayConstructionList = function (department) {
        $scope.TodayConstructionList = [];
        $http({
            method: 'POST',
            url: '../Construction/GetTodayConstructionList',
            data: { "DepId": department }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.TodayConstructionList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                alertify.alert("请检查网络");
            });
    }
    $scope.SelectConstructionForm = function (model) {
        var tempmodel = JSON.stringify(model);
        var cmodel = JSON.parse(tempmodel);
        $scope.AddModel.DepartmentId = cmodel.DepartmentId + "";
        $scope.AddModel.CompanyId = cmodel.CompanyId + "";
        $scope.GetConstructionCompany('Company_Select', cmodel.CompanyId);
        $scope.AddModel.ApplyDepContract = cmodel.ApplyDepContract;
        $scope.AddModel.ApplyDepContact = cmodel.ApplyDepContract;
        $scope.AddModel.ApplyDepPhone = cmodel.ApplyDepPhone;
        $scope.AddModel.Reponsible = cmodel.Reponsible;
        $scope.AddModel.Responsible = cmodel.Reponsible;
        $scope.AddModel.ProjectName = cmodel.ProjectName;
        $scope.AddModel.Phone = cmodel.Phone;
        $scope.AddModel.LocalDepContract = cmodel.LocalDepContract;
        $scope.AddModel.LocalDepContact = cmodel.LocalDepContract;
        $scope.AddModel.LoalDepPhone = cmodel.LoalDepPhone;
        $scope.AddModel.LocalDepPhone = cmodel.LoalDepPhone;
        $scope.AddModel.LocalDepId = cmodel.LocalDepId;
        $scope.BindDepartmentList('LocalDep_Select', cmodel.LocalDepId);
        //$scope.AddModel.StartTime = $filter('JSONDate')(cmodel.StartTime, 'yyyy-MM-dd HH:mm');
        //$scope.AddModel.EndTime = $filter('JSONDate')(cmodel.EndTime, 'yyyy-MM-dd HH:mm');
        //$scope.GetLocationList($scope.AddModel.FactoryAreaId, "Location_Select");
        $scope.AddModel.ConstructionId = cmodel.Id;
        $scope.AddModel.ConstructionFormNo = cmodel.FormNo;
        $scope.AddModel.FactoryAreaId = cmodel.FactoryAreaId + "";
        $scope.GetConstructionFormLocation(model.Id, $scope.AddModel.FactoryAreaId);
    }

    //地图标记点
    $scope.GetLocationList = function (factory, control, defaultvalue) {

        $scope.DataLoading = true;
        $("#" + control).empty();
        $("#" + control).append('   <option value="" selected>选择地点</option>');
        $http({
            method: 'POST',
            url: '../Construction/GetLocationList',
            data: { "FactoryAreaId": factory }
        })
            .success(function (ret) {
                $scope.DataLoading = false;
                common.success($scope, null, ret);
                $.each(ret.Data, function (i, n) {
                    if (defaultvalue != null) {
                        if (n.Id == defaultvalue)
                            $("#" + control).append($compile(" <option selected='selected' value=\"" + n.Id + "\">" + n.SimpleName + "</option>")($scope));
                        else
                            $("#" + control).append($compile(" <option value=\"" + n.Id + "\">" + n.SimpleName + "</option>")($scope));
                    }
                    else {
                        $("#" + control).append($compile(" <option value=\"" + n.Id + "\">" + n.SimpleName + "</option>")($scope));
                    }
                })
                $("#" + control).selectpicker('refresh');
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });

    }
    $scope.GetConstructionCompany = function (control, defaultvalue) {
        $scope.DataLoading = true;
        $("#" + control).empty();
        $("#" + control).append('    <option value="" selected>选择施工单位</option>');
        $http({
            method: 'POST',
            url: '../Construction/GetConstructionCompany'
        })
            .success(function (ret) {
                $scope.DataLoading = false;
                common.success($scope, null, ret);
                $.each(ret.Data, function (i, n) {
                    if (defaultvalue != null) {
                        if (n.Id == defaultvalue)
                            $("#" + control).append($compile(" <option selected='selected' value=\"" + n.Id + "\">" + n.CompanyName + "</option>")($scope));
                        else
                            $("#" + control).append($compile(" <option value=\"" + n.Id + "\">" + n.CompanyName + "</option>")($scope));
                    }
                    else {
                        $("#" + control).append($compile(" <option value=\"" + n.Id + "\">" + n.CompanyName + "</option>")($scope));
                    }
                })
                $("#" + control).selectpicker('refresh');
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });
    }
    $scope.GetFireOperateType = function () {
        $scope.TypeList = [];
        $http({
            method: 'POST',
            url: '../Construction/GetFireOperateType'
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.TypeList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                alertify.alert("请检查网络");
            });
    }

    $scope.GetFireOperateLevel = function () {
        $scope.LevelList = [];
        $http({
            method: 'POST',
            url: '../Construction/GetFireOperateLevel'
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.LevelList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                alertify.alert("请检查网络");
            });
    }

    $scope.GetFireOperateSolution = function () {
        $scope.SolutionList = [];
        $http({
            method: 'POST',
            url: '../Construction/GetFireOperateSafetySolution'
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.SolutionList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                alertify.alert("请检查网络");
            });
    }

    //地图标记点
    $scope.GetFireOperateWorker = function (control, defaultvalue) {
        $("#" + control).selectpicker({
            noneSelectedText: "选择动火人"
        });
        $scope.DataLoading = true;
        $("#" + control).empty();

        $http({
            method: 'POST',
            url: '../Construction/GetFireOperateWorker'
        })
            .success(function (ret) {
                $scope.DataLoading = false;
                common.success($scope, null, ret);
                $.each(ret.Data, function (i, n) {
                    if (defaultvalue != null) {
                        if (defaultvalue.includes(n.Id))
                            $("#" + control).append($compile("<option selected='selected' value=\"" + n.Id + "\">" + (n.WorkerName + "(" + n.IdCardNo + ")") + "</option>")($scope));
                        else
                            $("#" + control).append($compile("<option value=\"" + n.Id + "\">" + (n.WorkerName + "(" + n.IdCardNo + ")") + "</option>")($scope));
                    }
                    else {
                        $("#" + control).append($compile("<option value=\"" + n.Id + "\">" + (n.WorkerName + "(" + n.IdCardNo + ")") + "</option>")($scope));
                    }
                })
                $("#" + control).selectpicker('refresh');
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });
    }

    $scope.SaveFireOperateForm = function (model, status, page) {
        if (model.ConstructionId == null) {
            alertify.alert("请选择施工作业！");
            return false;
        }
        model.Status = status;
        model.LocationId = $("#Location_Select").val();
        var types = '';
        var solution = '';
        var typelist = $('[id^="chk_type_"]');
        for (var i = 0; i < typelist.length; i++) {
            if ($(typelist[i]).prop("checked")) {
                types += typelist[i].id.replace("chk_type_", "") + ",";
            }
        }
        model.TypeId = types;
        var solutionlist = $('[id^="chk_solution_"]');
        for (var i = 0; i < solutionlist.length; i++) {
            if ($(solutionlist[i]).prop("checked")) {
                solution += solutionlist[i].id.replace("chk_solution_", "") + ",";
            }
        }
        model.TypeId = types;
        model.SolutionId = solution;
        var files = document.querySelector('input[type=file]').files;
        if (status == 1) {
            if (model.FactoryAreaId == null || model.FactoryAreaId == '') {
                alertify.alert("请选择厂区！");
                return false;
            }
            if (model.CompanyId == null || model.CompanyId == '') {
                alertify.alert("请选择施工单位！");
                return false;
            }
            if (model.Content == null || model.Content == '') {
                alertify.alert("请输入动火项目！");
                return false;
            }
            //if (model.LocationId == null || model.LocationId == '') {
            //    alertify.alert("请选择动火地点！");
            //    return false;
            //}
            //if (model.LocationDetail == null || model.LocationDetail == '') {
            //    alertify.alert("请输入详细位置！");
            //    return false;
            //}
            if (model.LevelId == null || model.LevelId == '') {
                alertify.alert("请选择动火级别！");
                return false;
            }
            if (model.TypeId == null || model.TypeId == '') {
                alertify.alert("请选择动火类别！");
                return false;
            }
            if (model.FireOperateId == null) {
                alertify.alert("请选择动火人");
                return false;
            }
            if (model.CompanyManager == null || model.CompanyManager == '') {
                alertify.alert("请输入施工单位监护人");
                return false;
            }
            if (model.CompanyManagerPhone == null || model.CompanyManagerPhone == '') {
                alertify.alert("请输入施工单位监护人联系方式");
                return false;
            }
            if (model.Reponsible == null || model.Reponsible == '') {
                alertify.alert("请输入施工单位现场负责人");
                return false;
            }
            if (model.Phone == null || model.Phone == '') {
                alertify.alert("请输入施工单位现场负责人联系方式");
                return false;
            }
            if (model.ApplyDepContract == null || model.ApplyDepContract == '') {
                alertify.alert("请输入申请部门现场监护人");
                return false;
            }
            if (model.ApplyDepPhone == null || model.ApplyDepPhone == '') {
                alertify.alert("请输入申请部门现场监护人联系方式");
                return false;
            }
            if (model.LocalDepContract == null || model.LocalDepContract == '') {
                alertify.alert("请输入属地联系人");
                return false;
            }
            if (model.LoalDepPhone == null || model.LoalDepPhone == '') {
                alertify.alert("请输入属地联系人联系方式");
                return false;
            }
            if (model.SolutionId == null || model.SolutionId == '') {
                alertify.alert("请选择安全措施");
                return false;
            }
            if (model.StartTime == null || model.StartTime == '') {
                alertify.alert("请输入施工开始时间！");
                return false;
            }
            if (model.EndTime == null || model.EndTime == '') {
                alertify.alert("请输入施工结束时间！");
                return false;
            }
        }
        $scope.DataLoading = true;
        $("#btn_close_notice").click();
        Upload.upload({
            method: 'POST',
            url: '../Construction/AddFireOperateForm',
            data: { 'model': model, 'OperateList': model.FireOperateId, 'LocationList': $scope.LocationModel },
            file: files
        }).success(function (ret) {
            $scope.DataLoading = false;
            if (ret.Code == 100) {
                if (model.Status == 0) {
                    alertify.alert("保存成功！");
                }
                else {
                    alertify.alert("提交成功！");
                }
                setTimeout(function () {
                    window.location.href = "FireOperateFormPageList?page=" + page;
                }, 3000);
            } else
                alertify.alert(ret.Msg);
        })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                common.error($scope);
            })
    }


    $scope.GetFireOperateFormPageList = function (model, page, isload) {
        $scope.DataList = [];
        $scope.DataLoading = true;
        var condition = JSON.parse(sessionStorage.getItem(window.location.pathname));
        if (isload && condition) {
            model = condition;
            $scope.SearchModel = condition;
        } else {
            sessionStorage.setItem(window.location.pathname, JSON.stringify(model));
        }
        $http({
            method: 'POST',
            url: '../Construction/GetFireOperateFormPageList',
            data: { 'model': model, 'page': page }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.TotalCount = ret.Data.TotalItemCount;
                    $scope.DataList = ret.Data.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                alertify.alert("请检查网络");
            });

        $http({
            method: 'POST',
            url: '../Construction/GetFireOperateTaskCount',
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    $scope.TaskCount = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                alertify.alert("请检查网络");
            });
    }

    $scope.GetFireOperateForm = function (Id) {
        $scope.Loading = true;
        $scope.EditModel = {};
        $http({
            method: 'POST',
            url: '../Construction/GetFireOperateForm',
            data: { "Id": Id }
        })
            .success(function (ret) {
                $scope.Loading = false;
                common.success($scope, null, ret);
                $scope.GetFireOperateFormWorkList(Id);
                $scope.EditModel = ret.Data;
                $scope.GetApproveComment(ret.Data.Status);
                $scope.EditModel.FactoryAreaId = $scope.EditModel.FactoryAreaId + '';
                $scope.EditModel.LevelId = $scope.EditModel.LevelId + '';
                $scope.BindDepartmentList('Dep_Select', $scope.EditModel.DepartmentId);
                $scope.GetConstructionCompany('Company_Select', $scope.EditModel.CompanyId);
                $scope.BindDepartmentList('LocalDep_Select',$scope.EditModel.LocalDepId);
                $scope.GetLocationList($scope.EditModel.FactoryAreaId, 'Location_Select', $scope.EditModel.LocationId);
                $scope.EditModel.ApplyDate = $filter('JSONDate')($scope.EditModel.ApplyDate, 'yyyy-MM-dd');
                $scope.EditModel.StartTime = $filter('JSONDate')($scope.EditModel.StartTime, 'yyyy-MM-dd HH:mm');
                $scope.EditModel.EndTime = $filter('JSONDate')($scope.EditModel.EndTime, 'yyyy-MM-dd HH:mm');
                $scope.EditModel.CreateTime = $filter('JSONDate')($scope.EditModel.CreateTime, 'yyyy-MM-dd HH:mm:ss');
                if ($scope.EditModel.TypeId != null) {
                    var typeArray = $scope.EditModel.TypeId.split(',');
                    setTimeout(function () {
                        for (var i = 0; i < typeArray.length; i++) {
                            $("#chk_type_" + typeArray[i]).attr("checked", true);
                        }
                        $("#txt_type_" + 5).val($scope.EditModel.OtherType);
                    }, 3000);
                }
                if ($scope.EditModel.SolutionId != null) {
                    var solutionArray = $scope.EditModel.SolutionId.split(',');
                    setTimeout(function () {
                        for (var i = 0; i < solutionArray.length; i++) {
                            $("#chk_solution_" + solutionArray[i]).attr("checked", true);
                        }
                    }, 3000);
                }

                $scope.GetConstructionFormLocation($scope.EditModel.Id, $scope.EditModel.FactoryAreaId);
                $scope.Loading = false;


            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });
    }

    $scope.GetFireOperateFormDetail = function (Id) {
        $scope.Loading = true;
        $scope.EditModel = {};
        $http({
            method: 'POST',
            url: '../Construction/GetFireOperateForm',
            data: { "Id": Id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.GetFireOperateFormWorkList(Id);
                $scope.DetailModel = ret.Data;
                $scope.DetailModel.FactoryAreaId = $scope.DetailModel.FactoryAreaId + '';

                $scope.DetailModel.LevelId = $scope.DetailModel.LevelId + '';
                $scope.BindDepartmentList('Dep_Select', $scope.DetailModel.DepartmentId);
                $scope.GetConstructionCompany('Company_Select', $scope.DetailModel.CompanyId);
                $scope.GetLocationList($scope.DetailModel.FactoryAreaId, 'Location_Select', $scope.DetailModel.LocationId);
                $scope.DetailModel.ApplyDate = $filter('JSONDate')($scope.DetailModel.ApplyDate, 'yyyy年MM月dd日');
                $scope.DetailModel.StartTime = $filter('JSONDate')($scope.DetailModel.StartTime, 'yyyy年MM月dd日HH时mm分');
                $scope.DetailModel.EndTime = $filter('JSONDate')($scope.DetailModel.EndTime, 'yyyy年MM月dd日HH时mm分');
                $scope.DetailModel.CreateTime = $filter('JSONDate')($scope.DetailModel.CreateTime, 'yyyy-MM-dd HH:mm:ss');
                if ($scope.DetailModel.TypeId != null) {
                    var typeArray = $scope.DetailModel.TypeId.split(',');
                    setTimeout(function () {
                        for (var i = 0; i < typeArray.length; i++) {
                            $("#chk_type_" + typeArray[i]).attr("checked", true);
                        }
                        for (var i = 0; i < typeArray.length; i++) {
                            $("#chk_type2_" + typeArray[i]).attr("checked", true);
                        }
                        $("#txt_type_" + 5).val($scope.DetailModel.OtherType);
                    }, 3000);
                }
                if ($scope.DetailModel.SolutionId != null) {
                    var solutionArray = $scope.DetailModel.SolutionId.split(',');
                    setTimeout(function () {
                        $("#chk_level_" + $scope.DetailModel.LevelId).attr("checked", true);
                        for (var i = 0; i < solutionArray.length; i++) {
                            $("#chk_solution_" + solutionArray[i]).attr("checked", true);
                        }
                        for (var i = 0; i < solutionArray.length; i++) {
                            $("#chk_solution2_" + solutionArray[i]).attr("checked", true);
                        }
                    }, 3000);
                }

                $scope.Loading = false;

            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });
    }

    $scope.GetFireOperateFormWorkList = function (Id) {
        $http({
            method: 'POST',
            url: '../Construction/GetFireOperateFormWorker',
            data: { "Id": Id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.GetFireOperateWorker("Worker_Select", ret.Data);
                    $scope.EditModel.FireOperateId = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                alertify.alert("请检查网络");
            });

    }

    $scope.EditFireOperateForm = function (model, status, page) {
        if (model.ConstructionId == null) {
            alertify.alert("请选择施工作业！");
            return false;
        }
        model.Status = status;
        model.LocationId = $("#Location_Select").val();
        var types = '';
        var solution = '';
        var typelist = $('[id^="chk_type_"]');
        for (var i = 0; i < typelist.length; i++) {
            if ($(typelist[i]).prop("checked")) {
                types += typelist[i].id.replace("chk_type_", "") + ",";
            }
        }
        model.TypeId = types;
        var solutionlist = $('[id^="chk_solution_"]');
        for (var i = 0; i < solutionlist.length; i++) {
            if ($(solutionlist[i]).prop("checked")) {
                solution += solutionlist[i].id.replace("chk_solution_", "") + ",";
            }
        }
        model.TypeId = types;
        model.SolutionId = solution;
        var files = document.querySelector('input[type=file]').files;
        if (status == 1) {
            if (model.FactoryAreaId == null || model.FactoryAreaId == '') {
                alertify.alert("请选择厂区！");
                return false;
            }
            if (model.CompanyId == null || model.CompanyId == '') {
                alertify.alert("请选择施工单位！");
                return false;
            }
            if (model.Content == null || model.Content == '') {
                alertify.alert("请输入动火项目！");
                return false;
            }
            if (model.LevelId == null || model.LevelId == '') {
                alertify.alert("请选择动火级别！");
                return false;
            }
            if (model.TypeId == null || model.TypeId == '') {
                alertify.alert("请选择动火类别！");
                return false;
            }
            if (model.FireOperateId == null) {
                alertify.alert("请选择动火人");
                return false;
            }
            if (model.CompanyManager == null || model.CompanyManager == '') {
                alertify.alert("请输入施工单位监护人");
                return false;
            }
            if (model.CompanyManagerPhone == null || model.CompanyManagerPhone == '') {
                alertify.alert("请输入施工单位监护人联系方式");
                return false;
            }
            if (model.Reponsible == null || model.Reponsible == '') {
                alertify.alert("请输入施工单位现场负责人");
                return false;
            }
            if (model.Phone == null || model.Phone == '') {
                alertify.alert("请输入施工单位现场负责人联系方式");
                return false;
            }
            if (model.ApplyDepContract == null || model.ApplyDepContract == '') {
                alertify.alert("请输入申请部门现场监护人");
                return false;
            }
            if (model.ApplyDepPhone == null || model.ApplyDepPhone == '') {
                alertify.alert("请输入申请部门现场监护人联系方式");
                return false;
            }
            if (model.LocalDepContract == null || model.LocalDepContract == '') {
                alertify.alert("请输入属地联系人");
                return false;
            }
            if (model.LoalDepPhone == null || model.LoalDepPhone == '') {
                alertify.alert("请输入属地联系人联系方式");
                return false;
            }
            if (model.SolutionId == null || model.SolutionId == '') {
                alertify.alert("请选择安全措施");
                return false;
            }
            if (model.StartTime == null || model.StartTime == '') {
                alertify.alert("请输入施工开始时间！");
                return false;
            }
            if (model.EndTime == null || model.EndTime == '') {
                alertify.alert("请输入施工结束时间！");
                return false;
            }
        }
        $scope.Loading = true;
        $("#btn_close_notice").click();
        Upload.upload({
            method: 'POST',
            url: '../Construction/EditFireOperateForm',
            data: { 'model': model, 'OperateList': model.FireOperateId, "LocationList": $scope.LocationModel },
            file: files
        }).success(function (ret) {
            $scope.Loading = false;
            if (ret.Code == 100) {
                if (model.Status == 0) {
                    alertify.alert("保存成功！");
                }
                else {
                    alertify.alert("提交成功！");
                }
                setTimeout(function () {
                    window.location.href = "FireOperateFormPageList?page=" + page;
                }, 3000);
            } else
                alertify.alert(ret.Msg);
        })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                common.error($scope);
            })
    }

    $scope.DeleteFireOperateForm = function (id, searchmodel, page) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认此操作？", function (ev) {
                ev.preventDefault();
                $scope.PageLoading = true;
                $http({
                    method: 'POST',
                    url: '../Construction/DeleteFireOperateForm',
                    data: { 'Id': id }
                })
                    .success(function (ret) {
                        common.success($scope, null, ret);
                        $scope.PageLoading = false;
                        if (ret.Code == 100) {
                            $scope.AlertBox("删除成功！");
                            $scope.GetFireOperateFormPageList(searchmodel, page);
                        }
                        else {
                            alertify.alert(ret.Msg);
                        }
                    })
                    .error(function (data, status, headers, config) {
                        alertify.alert("请检查网络");
                    });
            }, function (ev) {
                ev.preventDefault();
            });
    }

    $scope.InitTicketPage = function (Id, addmodel) {
        $scope.Loading = true;
        $http({
            method: 'POST',
            url: '../Construction/GetFireOperateForm',
            data: { "Id": Id }
        })
            .success(function (ret) {
                $scope.Loading = false;
                common.success($scope, null, ret);
                addmodel.ProjectName = ret.Data.Content;//项目名称
                addmodel.LevelId = ret.Data.LevelId + '';
                addmodel.DepartmentId = ret.Data.DepartmentId;
                addmodel.OperateStartTime = $filter('JSONDate')(ret.Data.ActualStartTime, 'yyyy-MM-dd HH:mm');
                addmodel.OperateEndTime = $filter('JSONDate')(ret.Data.EndTime, 'yyyy-MM-dd HH:mm');
                addmodel.X = ret.Data.X;
                addmodel.Y = ret.Data.Y;
                $scope.BindDepartmentList('Dep_Select2', ret.Data.DepartmentId);
                $scope.GetFireFighterList(ret.Data.FactoryAreaId, "Fireman_Select");
                var typeArray = ret.Data.TypeId.split(',');
                setTimeout(function () {
                    for (var i = 0; i < typeArray.length; i++) {
                        $("#chk_type2_" + typeArray[i]).attr("checked", true);
                    }
                    $("#txt_type_" + 5).val($scope.EditModel.OtherType);

                }, 3000);

                $scope.Loading = false;
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });
    }

    $scope.GetFireFighterList = function (factory, control, defaultvalue) {
        $("#" + control).selectpicker({
            noneSelectedText: "选择防火监督人"
        });
        $scope.DataLoading = true;
        $("#" + control).empty();

        $http({
            method: 'POST',
            url: '../Construction/GetFireFighterList',
            data: { "FactoryAreaId": factory }
        })
            .success(function (ret) {
                $scope.DataLoading = false;
                common.success($scope, null, ret);
                $.each(ret.Data, function (i, n) {
                    if (defaultvalue != null) {
                        if (defaultvalue.includes(n.Id))
                            $("#" + control).append($compile("<option selected='selected' value=\"" + n.Id + "\">" + (n.Name + "(" + n.Phone + ")") + "</option>")($scope));
                        else
                            $("#" + control).append($compile("<option value=\"" + n.Id + "\">" + (n.Name + "(" + n.Phone + ")") + "</option>")($scope));
                    }
                    else {
                        $("#" + control).append($compile("<option value=\"" + n.Id + "\">" + (n.Name + "(" + n.Phone + ")") + "</option>")($scope));
                    }
                })
                $("#" + control).selectpicker('refresh');
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });
    }

    $scope.SaveFireOperateTicket = function (model, page, id) {
        var types = '';
        var isoutdoor = '';
        var typelist = $('[id^="chk_type2_"]');
        for (var i = 0; i < typelist.length; i++) {
            if ($(typelist[i]).prop("checked")) {
                types += typelist[i].id.replace("chk_type2_", "") + ",";
            }
        }
        var isoutdoorlist = $('[id^="IsOutdoor_"]');
        for (var i = 0; i < isoutdoorlist.length; i++) {
            if ($(isoutdoorlist[i]).prop("checked")) {
                isoutdoor += isoutdoorlist[i].id.replace("IsOutdoor_", "") + ",";
            }
        }
        model.IsOutdoor = isoutdoor;
        model.OperateTypeId = types;
        model.Fireman = model.FiremanId.join();
        console.log(model);
        $scope.Loading = true;
        $http({
            method: 'POST',
            url: '../Construction/AddFireOperateTicket',
            data: { 'model': model },
        }).success(function (ret) {
            $scope.Loading = false;
            if (ret.Code == 100) {
                alertify.alert("提交成功！");
                setTimeout(function () {
                    window.location.href = "FireOperateTicketList?id=" + id + "&page=" + page;
                }, 3000);
            } else
                alertify.alert(ret.Msg);
        })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                common.error($scope);
            })
    }

    $scope.GetFireOperateTicketList = function (Id) {
        $http({
            method: 'POST',
            url: '../Construction/GetFireOperateTicketList',
            data: { "Id": Id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.DataList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                alertify.alert("请检查网络");
            });

    }
    $scope.GetConstructionStatus = function (Id) {
        $http({
            method: 'POST',
            url: '../Construction/GetConstructionStatus',
            data: { "Id": Id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.StatusData = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                alertify.alert("请检查网络");
            });

    }
    $scope.PrintTicket = function (id) {
        $scope.Loading = true;
        $http({
            method: 'POST',
            url: '../Construction/GetFireOperateTicketFile',
            data: { "Id": id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.Loading = false;
                if (ret.Code == 100) {
                    window.open(ret.Data, "_blank");
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                alertify.alert("请检查网络");
            });
    }

    $scope.SelectPosition = function (entity) {
        entity.X = Math.round($("#h_XAxis").val() * 100);
        entity.Y = Math.round($("#h_YAxis").val() * 100);
    }
    $scope.GetTicketId = function (model, id) {
        model.CompleteTime = null;
        model.Id = id;
    }
    $scope.UploadFireOperateTicket = function (model) {
        if (model.CompleteTime == null) {
            alertify.alert("请填写完成时间！");
            return false;
        }
        var files = document.querySelector('input[type=file]').files;
        Upload.upload({
            method: 'POST',
            url: '../Construction/UploadFireOperateTicket',
            data: { 'model': model },
            file: files
        }).success(function (ret) {
            $scope.DataLoading = false;
            if (ret.Code == 100) {
                if (model.Status == 0) {
                    alertify.alert("保存成功！");
                }
                else {
                    alertify.alert("提交成功！");
                }
                setTimeout(function () {

                }, 3000);
            } else
                alertify.alert(ret.Msg);
        })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                common.error($scope);
            })
    }
    $scope.GetFireTicketFile = function (Id) {
        $scope.TicketFileList = [];
        $http({
            method: 'POST',
            url: '../Construction/GeFireTicketFile',
            data: { 'Id': Id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.TicketFileList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                alertify.alert("请检查网络");
            });
    }
    $scope.GetTodayFireOperateList = function (department) {
        $scope.TodayFireOperateList = [];
        $http({
            method: 'POST',
            url: '../Construction/GetTodayFireOperateList',
            data: { "DepId": department}
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.TodayFireOperateList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                alertify.alert("请检查网络");
            });
    }

    //高处作业


    $scope.GetClimbWorkerList = function (control, defaultvalue) {
        $("#" + control).selectpicker({
            noneSelectedText: "选择作业人"
        });
        $scope.DataLoading = true;
        $("#" + control).empty();
        //if (defaultvalue == null || defaultvalue.length == 0) {
        //    $("#" + control).append('  <option value="" selected>选择作业人</option>');
        //}

        $http({
            method: 'POST',
            url: '../Construction/GetClimbWorker'
        })
            .success(function (ret) {
                $scope.DataLoading = false;
                common.success($scope, null, ret);
                $.each(ret.Data, function (i, n) {
                    if (defaultvalue != null) {
                        if (defaultvalue.includes(n.Id + ""))
                            $("#" + control).append($compile("<option selected='selected' value=\"" + n.Id + "\">" + (n.WorkerName + "(" + n.IdCardNo + ")") + "</option>")($scope));
                        else
                            $("#" + control).append($compile("<option value=\"" + n.Id + "\">" + (n.WorkerName + "(" + n.IdCardNo + ")") + "</option>")($scope));
                    }
                    else {
                        $("#" + control).append($compile("<option value=\"" + n.Id + "\">" + (n.WorkerName + "(" + n.IdCardNo + ")") + "</option>")($scope));
                    }
                })
                $("#" + control).selectpicker('refresh');
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });
    }

    $scope.GetClimbOperateEqu = function () {
        $scope.EquList = [];
        $http({
            method: 'POST',
            url: '../Construction/GetClimbConfig',
            data: { "Type": 1 }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.EquList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                alertify.alert("请检查网络");
            });
    }


    $scope.GetClimbOperateSafetySolution = function () {
        $scope.SolutionList = [];
        $http({
            method: 'POST',
            url: '../Construction/GetClimbConfig',
            data: { "Type": 2 }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.SolutionList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                alertify.alert("请检查网络");
            });
    }

    $scope.SaveClimbOperateForm = function (model, status, page) {
        if (model.ConstructionId == null) {
            alertify.alert("请选择施工作业！");
            return false;
        }
        model.Status = status;

        var equ = '';
        var equlist = $('[id^="chk_equ_"]');
        for (var i = 0; i < equlist.length; i++) {
            if ($(equlist[i]).prop("checked")) {
                equ += equlist[i].id.replace("chk_equ_", "") + ",";
            }
        }
        model.SafetyEquId = equ;
        var solution = '';
        var solutionlist = $('[id^="chk_solution_"]');
        for (var i = 0; i < solutionlist.length; i++) {
            if ($(solutionlist[i]).prop("checked")) {
                solution += solutionlist[i].id.replace("chk_solution_", "") + ",";
            }
        }
        model.SafetySolutionId = solution;

        var isoutdoor = '';
        var isoutdoorlist = $('[id^="IsOutdoor_"]');
        for (var i = 0; i < isoutdoorlist.length; i++) {
            if ($(isoutdoorlist[i]).prop("checked")) {
                isoutdoor += isoutdoorlist[i].id.replace("IsOutdoor_", "") + ",";
            }
        }
        model.IsOutdoor = isoutdoor;
        model.Wokers = $("#Worker_Select").val().join();

        var files = document.querySelector('input[type=file]').files;
        if (status == 1) {
            if (model.FactoryAreaId == null || model.FactoryAreaId == "") {
                alertify.alert("请选择厂区！");
                return false;
            }
            if (model.CompanyId == null || model.CompanyId == "") {
                alertify.alert("请选择作业单位！");
                return false;
            }
            if (model.Content == null || model.Content == "") {
                alertify.alert("请填写作业内容！");
                return false;
            }
            if ($scope.LocationModel == null || $scope.LocationModel.length == 0) {
                alertify.alert("请添加作业地点！");
                return false;
            }
            if ($("#Worker_Select").val().length == 0) {
                alertify.alert("请添加作业人！");
                return false;
            }
            if (model.Responsible == null || model.Responsible == "") {
                alertify.alert("请填写施工单位现场作业监护人！");
                return false;
            }
            if (model.Phone == null || model.Phone == "") {
                alertify.alert("请填写施工单位现场作业监护人联系方式！");
                return false;
            }
            if (model.ApplyDepContact == null || model.ApplyDepContact == "") {
                alertify.alert("请填写申请部门现场监护人！");
                return false;
            }
            if (model.ApplyDepPhone == null || model.ApplyDepPhone == "") {
                alertify.alert("请填写申请部门现场监护人联系方式！");
                return false;
            }
            if (model.LocalDepContact == null || model.LocalDepContact == "") {
                alertify.alert("请填写属地联系人！");
                return false;
            }
            if (model.LocalDepContact == null || model.LocalDepContact == "") {
                alertify.alert("请填写属地联系人联系方式！");
                return false;
            }
            if (isoutdoor == '') {
                alertify.alert("请选择室内室外！");
                return false;
            }
            if (equ == '') {
                alertify.alert("请选择安全设施！");
                return false;
            }
            if (solution == '') {
                alertify.alert("请选择安全措施！");
                return false;
            }

            if (model.StartTime == null || model.StartTime == '') {
                alertify.alert("请输入施工开始时间！");
                return false;
            }
            if (model.EndTime == null || model.EndTime == '') {
                alertify.alert("请输入施工结束时间！");
                return false;
            }
        }
        $scope.DataLoading = true;
        $("#btn_close_notice").click();
        Upload.upload({
            method: 'POST',
            url: '../Construction/AddClimbOperateForm',
            data: { 'model': model, 'LocationList': $scope.LocationModel },
            file: files
        }).success(function (ret) {
            $scope.DataLoading = false;
            if (ret.Code == 100) {
                if (model.Status == 0) {
                    alertify.alert("保存成功！");
                }
                else {
                    alertify.alert("提交成功！");
                }
                setTimeout(function () {
                    window.location.href = "ClimbOperateFormPageList?page=" + page;
                }, 3000);
            } else
                alertify.alert(ret.Msg);
        })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                common.error($scope);
            })
    }

    $scope.GetClimbOperateFormPageList = function (model, page, isload) {
        $scope.DataList = [];
        $scope.DataLoading = true;
        var condition = JSON.parse(sessionStorage.getItem(window.location.pathname));
        if (isload && condition) {
            model = condition;
            $scope.SearchModel = condition;
        } else {
            sessionStorage.setItem(window.location.pathname, JSON.stringify(model));
        }

        $http({
            method: 'POST',
            url: '../Construction/GetClimbOperateFormPageList',
            data: { 'model': model, 'page': page }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    $scope.TotalCount = ret.Data.TotalItemCount;
                    $scope.DataList = ret.Data.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                alertify.alert("请检查网络");
            });

        $http({
            method: 'POST',
            url: '../Construction/GetClimbOperateTaskCount',
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    $scope.TaskCount = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                alertify.alert("请检查网络");
            });
    }

    $scope.GetClimbOperateForm = function (Id) {
        $scope.Loading = true;
        $scope.EditModel = {};
        $http({
            method: 'POST',
            url: '../Construction/GetClimbOperateForm',
            data: { "Id": Id }
        })
            .success(function (ret) {
                $scope.Loading = false;
                common.success($scope, null, ret);
                $scope.EditModel = ret.Data;
                $scope.EditModel.FactoryAreaId = $scope.EditModel.FactoryAreaId + '';
                $scope.EditModel.LevelId = $scope.EditModel.LevelId + '';
                $scope.BindDepartmentList('Dep_Select', $scope.EditModel.DepartmentId);
                $scope.BindDepartmentList('LocalDep_Select', $scope.EditModel.LocalDepId);
                $scope.GetConstructionCompany('Company_Select', $scope.EditModel.CompanyId);
                $scope.GetLocationList($scope.EditModel.FactoryAreaId, 'Location_Select', $scope.EditModel.LocationId);
                $scope.EditModel.ApplyDate = $filter('JSONDate')($scope.EditModel.ApplyDate, 'yyyy-MM-dd');
                $scope.EditModel.StartTime = $filter('JSONDate')($scope.EditModel.StartTime, 'yyyy-MM-dd HH:mm');
                $scope.EditModel.EndTime = $filter('JSONDate')($scope.EditModel.EndTime, 'yyyy-MM-dd HH:mm');
                $scope.EditModel.CreateTime = $filter('JSONDate')($scope.EditModel.CreateTime, 'yyyy-MM-dd HH:mm:ss');
                if ($scope.EditModel.SafetyEquId != null) {
                    var typeArray = $scope.EditModel.SafetyEquId.split(',');
                    setTimeout(function () {
                        for (var i = 0; i < typeArray.length; i++) {
                            $("#chk_equ_" + typeArray[i]).attr("checked", true);
                        }

                        for (var i = 0; i < typeArray.length; i++) {
                            $("#chk_equ2_" + typeArray[i]).attr("checked", true);
                        }
                    }, 3000);
                }
                if ($scope.EditModel.SafetySolutionId != null) {
                    var solutionArray = $scope.EditModel.SafetySolutionId.split(',');
                    setTimeout(function () {
                        for (var i = 0; i < solutionArray.length; i++) {
                            $("#chk_solution_" + solutionArray[i]).attr("checked", true);
                        }

                        for (var i = 0; i < solutionArray.length; i++) {
                            $("#chk_solution2_" + solutionArray[i]).attr("checked", true);
                        }
                    }, 3000);
                }
                $scope.GetClimbWorkerList('Worker_Select', $scope.EditModel.Wokers.split(","));


                if ($scope.EditModel.IsOutdoor != null) {
                    var isoutdoorArray = $scope.EditModel.IsOutdoor.split(',');
                    for (var i = 0; i < isoutdoorArray.length; i++) {
                        $("#IsOutdoor_" + isoutdoorArray[i]).attr("checked", true);
                    }

                    for (var i = 0; i < isoutdoorArray.length; i++) {
                        $("#IsOutdoor2_" + isoutdoorArray[i]).attr("checked", true);
                    }
                }

                $scope.GetConstructionFormLocation($scope.EditModel.Id, $scope.EditModel.FactoryAreaId);
                $scope.Loading = false;


            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });
    }

    $scope.GetClimbOperateDetailForm = function (Id) {
        $scope.Loading = true;
        $scope.DetailModel = {};
        $http({
            method: 'POST',
            url: '../Construction/GetClimbOperateForm',
            data: { "Id": Id }
        })
            .success(function (ret) {
                $scope.Loading = false;
                common.success($scope, null, ret);
                $scope.DetailModel = ret.Data;
                $scope.GetApproveComment($scope.DetailModel.Status);
                if ($scope.DetailModel.SafetyEquId != null) {
                    var typeArray = $scope.DetailModel.SafetyEquId.split(',');
                    setTimeout(function () {
                        for (var i = 0; i < typeArray.length; i++) {
                            $("#chk_equ_" + typeArray[i]).attr("checked", true);
                        }

                        for (var i = 0; i < typeArray.length; i++) {
                            $("#chk_equ2_" + typeArray[i]).attr("checked", true);
                        }
                    }, 3000);
                }
                if ($scope.DetailModel.SafetySolutionId != null) {
                    var solutionArray = $scope.DetailModel.SafetySolutionId.split(',');
                    setTimeout(function () {
                        for (var i = 0; i < solutionArray.length; i++) {
                            $("#chk_solution_" + solutionArray[i]).attr("checked", true);
                        }

                        for (var i = 0; i < solutionArray.length; i++) {
                            $("#chk_solution2_" + solutionArray[i]).attr("checked", true);
                        }
                    }, 3000);
                }
                $scope.GetClimbWorkerList('Worker_Select', $scope.DetailModel.Wokers.split(","));


                if ($scope.DetailModel.IsOutdoor != null) {
                    var isoutdoorArray = $scope.DetailModel.IsOutdoor.split(',');
                    for (var i = 0; i < isoutdoorArray.length; i++) {
                        $("#IsOutdoor_" + isoutdoorArray[i]).attr("checked", true);
                    }

                    for (var i = 0; i < isoutdoorArray.length; i++) {
                        $("#IsOutdoor2_" + isoutdoorArray[i]).attr("checked", true);
                    }
                }

                $scope.Loading = false;


            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });
    }

    $scope.EditClimbOperateForm = function (model, status, page) {
        if (model.ConstructionId == null) {
            alertify.alert("请选择施工作业！");
            return false;
        }
        model.Status = status;

        var equ = '';
        var equlist = $('[id^="chk_equ_"]');
        for (var i = 0; i < equlist.length; i++) {
            if ($(equlist[i]).prop("checked")) {
                equ += equlist[i].id.replace("chk_equ_", "") + ",";
            }
        }
        model.SafetyEquId = equ;
        var solution = '';
        var solutionlist = $('[id^="chk_solution_"]');
        for (var i = 0; i < solutionlist.length; i++) {
            if ($(solutionlist[i]).prop("checked")) {
                solution += solutionlist[i].id.replace("chk_solution_", "") + ",";
            }
        }
        model.SafetySolutionId = solution;

        var isoutdoor = '';
        var isoutdoorlist = $('[id^="IsOutdoor_"]');
        for (var i = 0; i < isoutdoorlist.length; i++) {
            if ($(isoutdoorlist[i]).prop("checked")) {
                isoutdoor += isoutdoorlist[i].id.replace("IsOutdoor_", "") + ",";
            }
        }
        model.IsOutdoor = isoutdoor;
        if ($("#Worker_Select").val() != null && $("#Worker_Select").val().length > 0) {
            model.Wokers = $("#Worker_Select").val().join();
        }

        var files = document.querySelector('input[type=file]').files;
        if (status == 1) {
            if (model.FactoryAreaId == null || model.FactoryAreaId == "") {
                alertify.alert("请选择厂区！");
                return false;
            }
            if (model.CompanyId == null || model.CompanyId == "") {
                alertify.alert("请选择作业单位！");
                return false;
            }
            if (model.Content == null || model.Content == "") {
                alertify.alert("请填写作业内容！");
                return false;
            }
            if ($scope.LocationModel == null || $scope.LocationModel.length == 0) {
                alertify.alert("请添加作业地点！");
                return false;
            }
            if ($("#Worker_Select").val().length == 0) {
                alertify.alert("请添加作业人！");
                return false;
            }
            if (model.Responsible == null || model.Responsible == "") {
                alertify.alert("请填写施工单位现场作业监护人！");
                return false;
            }
            if (model.Phone == null || model.Phone == "") {
                alertify.alert("请填写施工单位现场作业监护人联系方式！");
                return false;
            }
            if (model.ApplyDepContact == null || model.ApplyDepContact == "") {
                alertify.alert("请填写申请部门现场监护人！");
                return false;
            }
            if (model.ApplyDepPhone == null || model.ApplyDepPhone == "") {
                alertify.alert("请填写申请部门现场监护人联系方式！");
                return false;
            }
            if (model.LocalDepContact == null || model.LocalDepContact == "") {
                alertify.alert("请填写属地联系人！");
                return false;
            }
            if (model.LocalDepContact == null || model.LocalDepContact == "") {
                alertify.alert("请填写属地联系人联系方式！");
                return false;
            }
            if (isoutdoor == '') {
                alertify.alert("请选择室内室外！");
                return false;
            }
            if (equ == '') {
                alertify.alert("请选择安全设施！");
                return false;
            }
            if (solution == '') {
                alertify.alert("请选择安全措施！");
                return false;
            }
            if (model.StartTime == null || model.StartTime == '') {
                alertify.alert("请输入施工开始时间！");
                return false;
            }
            if (model.EndTime == null || model.EndTime == '') {
                alertify.alert("请输入施工结束时间！");
                return false;
            }

        }

        $scope.DataLoading = true;
        $("#btn_close_notice").click();
        Upload.upload({
            method: 'POST',
            url: '../Construction/EditClimbOperateForm',
            data: { 'model': model, 'LocationList': $scope.LocationModel },
            file: files
        }).success(function (ret) {
            $scope.DataLoading = false;
            if (ret.Code == 100) {
                if (model.Status == 0) {
                    alertify.alert("保存成功！");
                }
                else {
                    alertify.alert("提交成功！");
                }
                setTimeout(function () {
                    window.location.href = "ClimbOperateFormPageList?page=" + page;
                }, 3000);
            } else
                alertify.alert(ret.Msg);
        })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                common.error($scope);
            })
    }

    $scope.ApproveClimbOperateForm = function (model, status, page) {
        var text = "通过";
        if (status == "false") {
            text = "驳回";
            if (model.Remark == null || model.Remark == "") {
                alertify.alert("请填写意见！");
                return false;
            }
        }
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认" + text + "该高处作业申请？", function (ev) {
                ev.preventDefault();
                $scope.Loading = true;
                model.IsApprove = status;
                $http({
                    method: 'POST',
                    url: '../Construction/ApproveClimbOperateForm',
                    data: { 'model': model }
                })
                    .success(function (ret) {
                        common.success($scope, null, ret);
                        $scope.Loading = false;
                        if (ret.Code == 100) {
                            alertify.alert(ret.Msg);
                            setTimeout(function () {
                                window.location.href = "ClimbOperateFormPageList?page=" + page;
                            }, 2000);

                        }
                        else {
                            alertify.alert(ret.Msg);
                        }
                    })
                    .error(function (data, status, headers, config) {
                        alertify.alert("请检查网络");
                    });
            }, function (ev) {
                ev.preventDefault();
            });
    }

    $scope.DeleteClimbOperateForm = function (id, searchmodel, page) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认此操作？", function (ev) {
                ev.preventDefault();
                $scope.PageLoading = true;
                $http({
                    method: 'POST',
                    url: '../Construction/DeleteClimbOperateForm',
                    data: { 'Id': id }
                })
                    .success(function (ret) {
                        common.success($scope, null, ret);
                        $scope.PageLoading = false;
                        if (ret.Code == 100) {
                            $scope.AlertBox("删除成功！");
                            $scope.GetClimbOperateFormPageList(searchmodel, page);
                        }
                        else {
                            alertify.alert(ret.Msg);
                        }
                    })
                    .error(function (data, status, headers, config) {
                        alertify.alert("请检查网络");
                    });
            }, function (ev) {
                ev.preventDefault();
            });
    }

    $scope.AddClimbOperateWorker = function (model) {
        $http({
            method: 'POST',
            url: '../Construction/AddClimbOperateWorker',
            data: { 'model': model }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    alertify.alert(ret.Msg);
                    $("#btn_close2").click();
                    $scope.GetClimbWorkerList('Worker_Select', $("#Worker_Select").val());
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                alertify.alert("请检查网络");
            });
    }


    $scope.GetApproveComment = function (status) {
        $scope.ApproveCommnet = {};
        $http({
            method: 'POST',
            url: '../Construction/GetApproveComment',
            data: { "Status": status }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    if (ret.Data != null && $scope.ApproveModel != null) {
                        $scope.ApproveModel.Remark = ret.Data.Comments;
                    }
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                alertify.alert("请检查网络");
            });
    }

    $scope.ClearCondition = function () {
        $scope.SearchModel = {};
    }

    $scope.ExportFireOperateCheckForm = function (Id) {
        $http({
            method: 'POST',
            url: '../Construction/ExportFireOperateCheckFile',
            data: { "Id": Id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    window.open(ret.Data, "_blank", "width=500,height=400,top=100,left=100");
                    //window.location.href = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                alertify.alert("请检查网络");
            });
    }

    $scope.InitConstructionDetailPage = function (Id) {
        $scope.GetConstructionType();
        $scope.GetConstructionDocType();
        $scope.GetConstructionDetail(Id);
        $scope.GetConstructionFileList(Id);
        $scope.GetApproveLogList(Id);
    }

    //有限空间
    $scope.AddLimitedSpaceOperateForm = function (model, status, page) {
        if (model.ConstructionId == null) {
            alertify.alert("请选择施工作业！");
            return false;
        }
        model.Status = status;

        //获取有限空间内有毒有害物质
        var harmful = '';
        var harmlist = $('[id^="chk_harmful_"]');
        for (var i = 0; i < harmlist.length; i++) {
            if ($(harmlist[i]).prop("checked")) {
                harmful += harmlist[i].id.replace("chk_harmful_", "") + ",";
            }
        }
        model.HarmfulFactor = harmful;


       //获取危险辨识结果
        var riskresult = '';
        var risklist = $('[id^="chk_riskresult_"]');
        for (var i = 0; i < risklist.length; i++) {
            if ($(risklist[i]).prop("checked")) {
                riskresult += risklist[i].id.replace("chk_riskresult_", "") + ",";
            }
        }
        model.RiskResult = riskresult;

        //获取个人防护用品
        var equ = '';
        var equlist = $('[id^="chk_equ_"]');
        for (var i = 0; i < equlist.length; i++) {
            if ($(equlist[i]).prop("checked")) {
                equ += equlist[i].id.replace("chk_equ_", "") + ",";
            }
        }
        model.PersonalProtection = equ;

        //获取应急器材
        var emergencyequ = '';
        var emergencequlist = $('[id^="chk_emergencyequ_"]');
        for (var i = 0; i < equlist.length; i++) {
            if ($(emergencequlist[i]).prop("checked")) {
                emergencyequ += emergencequlist[i].id.replace("chk_emergencyequ_", "") + ",";
            }
        }
        model.EmergencyEqu = emergencyequ;


        //获取安全措施
        var solution = '';
        var solutionlist = $('[id^="chk_solution_"]');
        for (var i = 0; i < solutionlist.length; i++) {
            if ($(solutionlist[i]).prop("checked")) {
                solution += solutionlist[i].id.replace("chk_solution_", "") + ",";
            }
        }
        model.SafetySolution = solution;

        //地上地下
        var isoutdoor = '';
        var isoutdoorlist = $('[id^="IsOutdoor_"]');
        for (var i = 0; i < isoutdoorlist.length; i++) {
            if ($(isoutdoorlist[i]).prop("checked")) {
                isoutdoor += isoutdoorlist[i].id.replace("IsOutdoor_", "") + ",";
            }
        }
        model.UnderGround = isoutdoor;

        //施工人
        if ($("#Worker_Select").val() != null) {
            model.Workers = $("#Worker_Select").val().join();
        }
        

        var files = document.querySelector('input[type=file]').files;
        if (status == 1) {
            if (model.FactoryAreaId == null || model.FactoryAreaId == "") {
                alertify.alert("请选择厂区！");
                return false;
            }
            if (model.CompanyId == null || model.CompanyId == "") {
                alertify.alert("请选择作业单位！");
                return false;
            }
            if (model.Content == null || model.Content == "") {
                alertify.alert("请填写作业内容！");
                return false;
            }
            if ($scope.LocationModel == null || $scope.LocationModel.length == 0) {
                alertify.alert("请添加作业地点！");
                return false;
            }
            if ($("#Worker_Select").val() == null || $("#Worker_Select").val() == "" || $("#Worker_Select").val().length == 0) {
                alertify.alert("请添加作业人！");
                return false;
            }
            if (model.Responsible == null || model.Responsible == "") {
                alertify.alert("请填写施工单位现场作业监护人！");
                return false;
            }
            if (model.Phone == null || model.Phone == "") {
                alertify.alert("请填写施工单位现场作业监护人联系方式！");
                return false;
            }
            if (model.ApplyDepContact == null || model.ApplyDepContact == "") {
                alertify.alert("请填写申请部门现场监护人！");
                return false;
            }
            if (model.ApplyDepPhone == null || model.ApplyDepPhone == "") {
                alertify.alert("请填写申请部门现场监护人联系方式！");
                return false;
            }
            if (model.LocalDepContact == null || model.LocalDepContact == "") {
                alertify.alert("请填写属地联系人！");
                return false;
            }
            if (model.LocalDepContact == null || model.LocalDepContact == "") {
                alertify.alert("请填写属地联系人联系方式！");
                return false;
            }
            if (isoutdoor == '') {
                alertify.alert("请选择地上地下！");
                return false;
            }
            if (harmful == '') {
                alertify.alert("请选择有限空间内有毒有害物质！");
                return false;
            }
            if (riskresult == '') {
                alertify.alert("请选择危险辨识结果！");
                return false;
            }
            if (equ == '') {
                alertify.alert("请选择个人防护用品！");
                return false;
            }
            if (emergencyequ == '') {
                alertify.alert("请选择应急器材！");
                return false;
            }
            if (solution == '') {
                alertify.alert("请选择安全措施！");
                return false;
            }

            if (model.StartTime == null || model.StartTime == '') {
                alertify.alert("请输入施工开始时间！");
                return false;
            }
            if (model.EndTime == null || model.EndTime == '') {
                alertify.alert("请输入施工结束时间！");
                return false;
            }
        }
        $scope.DataLoading = true;
        $("#btn_close_notice").click();
        Upload.upload({
            method: 'POST',
            url: '../Construction/AddLimitedSpaceOperateForm',
            data: { 'model': model, 'LocationList': $scope.LocationModel },
            file: files
        }).success(function (ret) {
            $scope.DataLoading = false;
            if (ret.Code == 100) {
                if (model.Status == 0) {
                    alertify.alert("保存成功！");
                }
                else {
                    alertify.alert("提交成功！");
                }
                setTimeout(function () {
                    window.location.href = "LimitedSpaceOperateFormPageList?page=" + page;
                }, 3000);
            } else
                alertify.alert(ret.Msg);
        })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                common.error($scope);
            })
    }
    

    $scope.AddLimitSpaceWorker = function (model) {
        $http({
            method: 'POST',
            url: '../Construction/AddLimitSpaceWorker',
            data: { 'model': model }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    alertify.alert(ret.Msg);
                    $("#btn_close2").click();
                    $scope.GetLimitSpaceWorker('Worker_Select', $("#Worker_Select").val());
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                alertify.alert("请检查网络");
            });
    }


    $scope.GetLimitSpaceWorker = function (control, defaultvalue) {
        $("#" + control).selectpicker({
            noneSelectedText: "选择作业人"
        });
        $scope.DataLoading = true;
        $("#" + control).empty();
        //if (defaultvalue == null || defaultvalue.length == 0) {
        //    $("#" + control).append('  <option value="" selected>选择作业人</option>');
        //}

        $http({
            method: 'POST',
            url: '../Construction/GetLimitSpaceWorker'
        })
            .success(function (ret) {
                $scope.DataLoading = false;
                common.success($scope, null, ret);
                $.each(ret.Data, function (i, n) {
                    if (defaultvalue != null) {
                        if (defaultvalue.includes(n.Id + ""))
                            $("#" + control).append($compile("<option selected='selected' value=\"" + n.Id + "\">" + (n.WorkerName + "(" + n.IdCardNo + ")") + "</option>")($scope));
                        else
                            $("#" + control).append($compile("<option value=\"" + n.Id + "\">" + (n.WorkerName + "(" + n.IdCardNo + ")") + "</option>")($scope));
                    }
                    else {
                        $("#" + control).append($compile("<option value=\"" + n.Id + "\">" + (n.WorkerName + "(" + n.IdCardNo + ")") + "</option>")($scope));
                    }
                })
                $("#" + control).selectpicker('refresh');
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });
    }
    $scope.GetPersonalProtect = function () {
        $scope.PersonalEquList = [];
        $http({
            method: 'POST',
            url: '../Construction/GetLimitSpaceConfig',
            data: { "Type": 1 }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.PersonalEquList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                alertify.alert("请检查网络");
            });
    }

    $scope.GetEmergencyEqu = function () {
        $scope.EmergencyEquList = [];
        $http({
            method: 'POST',
            url: '../Construction/GetLimitSpaceConfig',
            data: { "Type": 2 }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.EmergencyEquList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                alertify.alert("请检查网络");
            });
    }

    $scope.GetHarmful = function () {
        $scope.HarmfulList = [];
        $http({
            method: 'POST',
            url: '../Construction/GetLimitSpaceConfig',
            data: { "Type": 4 }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.HarmfulList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                alertify.alert("请检查网络");
            });
    }

    $scope.GetRiskList = function () {
        $scope.RiskList = [];
        $http({
            method: 'POST',
            url: '../Construction/GetLimitSpaceConfig',
            data: { "Type": 5 }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.RiskList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                alertify.alert("请检查网络");
            });
    }

    $scope.GetSafetySolution = function () {
        $scope.SafetySolutionList = [];
        $http({
            method: 'POST',
            url: '../Construction/GetLimitSpaceConfig',
            data: { "Type": 3 }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.SafetySolutionList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                alertify.alert("请检查网络");
            });
    }

    $scope.GetLimitSpaceOperateFormPageList = function (model, page, isload) {
        $scope.DataList = [];
        $scope.DataLoading = true;
        var condition = JSON.parse(sessionStorage.getItem(window.location.pathname));
        if (isload && condition) {
            model = condition;
            $scope.SearchModel = condition;
        } else {
            sessionStorage.setItem(window.location.pathname, JSON.stringify(model));
        }

        $http({
            method: 'POST',
            url: '../Construction/GetLimitSpaceOperateFormPageList',
            data: { 'model': model, 'page': page }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.TotalCount = ret.Data.TotalItemCount;
                    $scope.DataList = ret.Data.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                alertify.alert("请检查网络");
            });

        $http({
            method: 'POST',
            url: '../Construction/GetLimitSpaceOperateTaskCount',
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    $scope.TaskCount = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                alertify.alert("请检查网络");
            });
    }

    $scope.DeleteFireOperateTicket = function (id,formid ) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认此操作？", function (ev) {
                ev.preventDefault();
                $scope.PageLoading = true;
                $http({
                    method: 'POST',
                    url: '../Construction/DeleteFireOperateTicket',
                    data: { 'Id': id }
                })
                    .success(function (ret) {
                        common.success($scope, null, ret);
                        $scope.PageLoading = false;
                        if (ret.Code == 100) {
                            $scope.AlertBox("删除成功！");
                            $scope.GetFireOperateTicketList(formid);
                        }
                        else {
                            alertify.alert(ret.Msg);
                        }
                    })
                    .error(function (data, status, headers, config) {
                        alertify.alert("请检查网络");
                    });
            }, function (ev) {
                ev.preventDefault();
            });
    }

    $scope.DeleteLimitSpaceOperate = function (id, searchmodel, page) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认此操作？", function (ev) {
                ev.preventDefault();
                $scope.PageLoading = true;
                $http({
                    method: 'POST',
                    url: '../Construction/DeleteLimitSpaceOperate',
                    data: { 'Id': id }
                })
                    .success(function (ret) {
                        common.success($scope, null, ret);
                        $scope.PageLoading = false;
                        if (ret.Code == 100) {
                            $scope.AlertBox("删除成功！");
                            $scope.GetLimitSpaceOperateFormPageList(searchmodel, page);
                        }
                        else {
                            alertify.alert(ret.Msg);
                        }
                    })
                    .error(function (data, status, headers, config) {
                        alertify.alert("请检查网络");
                    });
            }, function (ev) {
                ev.preventDefault();
            });
    }

    $scope.GetLimitedSpaceOperateForm = function (Id) {
        $scope.Loading = true;
        $scope.EditModel = {};
        $http({
            method: 'POST',
            url: '../Construction/GetLimitedSpaceOperateForm',
            data: { "Id": Id }
        })
            .success(function (ret) {
                $scope.Loading = false;
                common.success($scope, null, ret);
                $scope.EditModel = ret.Data;
                $scope.EditModel.FactoryAreaId = $scope.EditModel.FactoryAreaId + '';
                $scope.EditModel.LevelId = $scope.EditModel.LevelId + '';
                $scope.BindDepartmentList('Dep_Select', $scope.EditModel.DepartmentId);
                $scope.BindDepartmentList('LocalDep_Select', $scope.EditModel.LocalDepId);
                $scope.GetConstructionCompany('Company_Select', $scope.EditModel.CompanyId);
                $scope.GetLocationList($scope.EditModel.FactoryAreaId, 'Location_Select', $scope.EditModel.LocationId);
                $scope.EditModel.ApplyDate = $filter('JSONDate')($scope.EditModel.ApplyDate, 'yyyy-MM-dd');
                $scope.EditModel.StartTime = $filter('JSONDate')($scope.EditModel.StartTime, 'yyyy-MM-dd HH:mm');
                $scope.EditModel.EndTime = $filter('JSONDate')($scope.EditModel.EndTime, 'yyyy-MM-dd HH:mm');
                $scope.EditModel.CreateTime = $filter('JSONDate')($scope.EditModel.CreateTime, 'yyyy-MM-dd HH:mm:ss');
                if ($scope.EditModel.HarmfulFactor != null) {
                    var harmfulArray = $scope.EditModel.HarmfulFactor.split(',');
                    setTimeout(function () {
                        for (var i = 0; i < harmfulArray.length; i++) {
                            $("#chk_harmful_" + harmfulArray[i]).attr("checked", true);
                        }

                        for (var i = 0; i < harmfulArray.length; i++) {
                            $("#chk_harmful2_" + harmfulArray[i]).attr("checked", true);
                        }
                    }, 3000);
                }
                if ($scope.EditModel.RiskResult != null) {
                    var risklArray = $scope.EditModel.RiskResult.split(',');
                    setTimeout(function () {
                        for (var i = 0; i < risklArray.length; i++) {
                            $("#chk_riskresult_" + risklArray[i]).attr("checked", true);
                        }

                        for (var i = 0; i < risklArray.length; i++) {
                            $("#chk_riskresult2_" + risklArray[i]).attr("checked", true);
                        }
                    }, 3000);
                }

                if ($scope.EditModel.PersonalProtection != null) {
                    var ppArray = $scope.EditModel.PersonalProtection.split(',');
                    setTimeout(function () {
                        for (var i = 0; i < ppArray.length; i++) {
                            $("#chk_equ_" + ppArray[i]).attr("checked", true);
                        }

                        for (var i = 0; i < ppArray.length; i++) {
                            $("#chk_equ2_" + ppArray[i]).attr("checked", true);
                        }
                    }, 3000);
                }

                if ($scope.EditModel.EmergencyEqu != null) {
                    var eeArray = $scope.EditModel.EmergencyEqu.split(',');
                    setTimeout(function () {
                        for (var i = 0; i < eeArray.length; i++) {
                            $("#chk_emergencyequ_" + eeArray[i]).attr("checked", true);
                        }

                        for (var i = 0; i < eeArray.length; i++) {
                            $("#chk_emergencyequ2_" + eeArray[i]).attr("checked", true);
                        }
                    }, 3000);
                }
                
                if ($scope.EditModel.SafetySolution != null) {
                    var solutionArray = $scope.EditModel.SafetySolution.split(',');
                    setTimeout(function () {
                        for (var i = 0; i < solutionArray.length; i++) {
                            $("#chk_solution_" + solutionArray[i]).attr("checked", true);
                        }

                        for (var i = 0; i < solutionArray.length; i++) {
                            $("#chk_solution2_" + solutionArray[i]).attr("checked", true);
                        }

                       
                    }, 3000);
                }

                

                $scope.GetLimitSpaceWorker('Worker_Select', $scope.EditModel.Workers.split(","));


                if ($scope.EditModel.UnderGround != null) {
                    var isoutdoorArray = $scope.EditModel.UnderGround.split(',');
                    for (var i = 0; i < isoutdoorArray.length; i++) {
                        $("#IsOutdoor_" + isoutdoorArray[i]).attr("checked", true);
                    }

                    for (var i = 0; i < isoutdoorArray.length; i++) {
                        $("#IsOutdoor2_" + isoutdoorArray[i]).attr("checked", true);
                    }
                }
                $scope.GetApproveComment($scope.EditModel.Status);
                $scope.GetConstructionFormLocation($scope.EditModel.Id, $scope.EditModel.FactoryAreaId);
                $scope.Loading = false;
                $scope.ShowPrint = true;

            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });
    }

    $scope.EditLimitSpaceOperateForm = function (model, status, page) {
        if (model.ConstructionId == null) {
            alertify.alert("请选择施工作业！");
            return false;
        }
        model.Status = status;

        //获取有限空间内有毒有害物质
        var harmful = '';
        var harmlist = $('[id^="chk_harmful_"]');
        for (var i = 0; i < harmlist.length; i++) {
            if ($(harmlist[i]).prop("checked")) {
                harmful += harmlist[i].id.replace("chk_harmful_", "") + ",";
            }
        }
        model.HarmfulFactor = harmful;


        //获取危险辨识结果
        var riskresult = '';
        var risklist = $('[id^="chk_riskresult_"]');
        for (var i = 0; i < risklist.length; i++) {
            if ($(risklist[i]).prop("checked")) {
                riskresult += risklist[i].id.replace("chk_riskresult_", "") + ",";
            }
        }
        model.RiskResult = riskresult;

        //获取个人防护用品
        var equ = '';
        var equlist = $('[id^="chk_equ_"]');
        for (var i = 0; i < equlist.length; i++) {
            if ($(equlist[i]).prop("checked")) {
                equ += equlist[i].id.replace("chk_equ_", "") + ",";
            }
        }
        model.PersonalProtection = equ;

        //获取应急器材
        var emergencyequ = '';
        var emergencequlist = $('[id^="chk_emergencyequ_"]');
        for (var i = 0; i < equlist.length; i++) {
            if ($(emergencequlist[i]).prop("checked")) {
                emergencyequ += emergencequlist[i].id.replace("chk_emergencyequ_", "") + ",";
            }
        }
        model.EmergencyEqu = emergencyequ;


        //获取安全措施
        var solution = '';
        var solutionlist = $('[id^="chk_solution_"]');
        for (var i = 0; i < solutionlist.length; i++) {
            if ($(solutionlist[i]).prop("checked")) {
                solution += solutionlist[i].id.replace("chk_solution_", "") + ",";
            }
        }
        model.SafetySolution = solution;

        //地上地下
        var isoutdoor = '';
        var isoutdoorlist = $('[id^="IsOutdoor_"]');
        for (var i = 0; i < isoutdoorlist.length; i++) {
            if ($(isoutdoorlist[i]).prop("checked")) {
                isoutdoor += isoutdoorlist[i].id.replace("IsOutdoor_", "") + ",";
            }
        }
        model.UnderGround = isoutdoor;

        //施工人
        if ($("#Worker_Select").val() != null) {
            model.Workers = $("#Worker_Select").val().join();
        }


        var files = document.querySelector('input[type=file]').files;
        if (status == 1) {
            if (model.FactoryAreaId == null || model.FactoryAreaId == "") {
                alertify.alert("请选择厂区！");
                return false;
            }
            if (model.CompanyId == null || model.CompanyId == "") {
                alertify.alert("请选择作业单位！");
                return false;
            }
            if (model.Content == null || model.Content == "") {
                alertify.alert("请填写作业内容！");
                return false;
            }
            if ($scope.LocationModel == null || $scope.LocationModel.length == 0) {
                alertify.alert("请添加作业地点！");
                return false;
            }
            if ($("#Worker_Select").val().length == 0) {
                alertify.alert("请添加作业人！");
                return false;
            }
            if (model.Responsible == null || model.Responsible == "") {
                alertify.alert("请填写施工单位现场作业监护人！");
                return false;
            }
            if (model.Phone == null || model.Phone == "") {
                alertify.alert("请填写施工单位现场作业监护人联系方式！");
                return false;
            }
            if (model.ApplyDepContact == null || model.ApplyDepContact == "") {
                alertify.alert("请填写申请部门现场监护人！");
                return false;
            }
            if (model.ApplyDepPhone == null || model.ApplyDepPhone == "") {
                alertify.alert("请填写申请部门现场监护人联系方式！");
                return false;
            }
            if (model.LocalDepContact == null || model.LocalDepContact == "") {
                alertify.alert("请填写属地联系人！");
                return false;
            }
            if (model.LocalDepContact == null || model.LocalDepContact == "") {
                alertify.alert("请填写属地联系人联系方式！");
                return false;
            }
            if (isoutdoor == '') {
                alertify.alert("请选择地上地下！");
                return false;
            }
            if (harmful == '') {
                alertify.alert("请选择有限空间内有毒有害物质！");
                return false;
            }
            if (riskresult == '') {
                alertify.alert("请选择危险辨识结果！");
                return false;
            }
            if (equ == '') {
                alertify.alert("请选择个人防护用品！");
                return false;
            }
            if (emergencyequ == '') {
                alertify.alert("请选择应急器材！");
                return false;
            }
            if (solution == '') {
                alertify.alert("请选择安全措施！");
                return false;
            }
            if (model.StartTime == null || model.StartTime == '') {
                alertify.alert("请输入施工开始时间！");
                return false;
            }
            if (model.EndTime == null || model.EndTime == '') {
                alertify.alert("请输入施工结束时间！");
                return false;
            }
        }
        $scope.DataLoading = true;
        $("#btn_close_notice").click();
        Upload.upload({
            method: 'POST',
            url: '../Construction/EditLimitSpaceOperateForm',
            data: { 'model': model, 'LocationList': $scope.LocationModel },
            file: files
        }).success(function (ret) {
            $scope.DataLoading = false;
            if (ret.Code == 100) {
                if (model.Status == 0) {
                    alertify.alert("保存成功！");
                }
                else {
                    alertify.alert("提交成功！");
                }
                setTimeout(function () {
                    window.location.href = "LimitedSpaceOperateFormPageList?page=" + page;
                }, 3000);
            } else
                alertify.alert(ret.Msg);
        })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                common.error($scope);
            })
    }

    $scope.ApproveLimitSpaceOperateForm = function (model, status, page) {
        var text = "通过";
        if (status == "false") {
            text = "驳回";
            if (model.Remark == null || model.Remark == "") {
                alertify.alert("请填写意见！");
                return false;
            }
        }
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认" + text + "该有限空间作业申请？", function (ev) {
                ev.preventDefault();
                $scope.Loading = true;
                model.IsApprove = status;
                $http({
                    method: 'POST',
                    url: '../Construction/ApproveLimitSpaceOperateForm',
                    data: { 'model': model }
                })
                    .success(function (ret) {
                        common.success($scope, null, ret);
                        $scope.Loading = false;
                        if (ret.Code == 100) {
                            alertify.alert(ret.Msg);
                            setTimeout(function () {
                                window.location.href = "LimitedSpaceOperateFormPageList?page=" + page;
                            }, 2000);

                        }
                        else {
                            alertify.alert(ret.Msg);
                        }
                    })
                    .error(function (data, status, headers, config) {
                        alertify.alert("请检查网络");
                    });
            }, function (ev) {
                ev.preventDefault();
            });
    }

    $scope.GetTodayLimitSapceList = function (department) {
        $scope.TodayClimbFormList = [];
        $http({
            method: 'POST',
            url: '../Construction/GetTodayLimitSapceList',
            data: { "DepId": department }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.TodayClimbFormList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                alertify.alert("请检查网络");
            });
    }

});

